package cn.com.fone.modules.user.rest;

import cn.com.fone.base.entity.BaseEntity;
import cn.com.fone.modules.user.entity.FoneUserEntity;
import cn.com.fone.modules.user.service.impl.FoneUserServiceImpl;
import cn.com.fone.util.PageUtils;
import cn.com.fone.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author hu.rl
 * @email runlihu@163.com
 * @date 2018-06-10 16:24:41
 */
@RestController
@RequestMapping("/user")
@RefreshScope
public class FoneUserController extends BaseEntity {

    private static final Logger LOGGER = LoggerFactory.getLogger(FoneUserController.class);

    @Autowired
    private FoneUserServiceImpl foneUserService;


    @Value("${username}")
    private String userName;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        System.out.println("=================" + userName);
        PageUtils page = foneUserService.queryPage(params);

        LOGGER.info("获取远程配置，用户名为：userName = {}", userName);
        return R.ok().put("userName", userName).put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        FoneUserEntity foneUser = foneUserService.selectById(id);
        return R.ok().put("foneUser", foneUser);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FoneUserEntity foneUser) {
        foneUserService.insert(foneUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FoneUserEntity foneUser) {
        foneUserService.updateById(foneUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        foneUserService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
